package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author: ylc
 * @Description:通过JDBC进行DML操作
 * @Date Created in 2022-05-09 19:35
 */
public class JDBCDemo1 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //1.初始化给定的类，实际上是在静态代码块中通过JDBC中的DriverManger注册驱动
            //DriverManager.registerDriver("com.mysql.jdbc.Driver");
            Class.forName("com.mysql.jdbc.Driver");
            //2.通过驱动获取数据库连接
            conn = DriverManager.getConnection("jdbc:mysql:///study","root","root");
            //3.获取sql对象
            stmt = conn.createStatement();
            String sql = "update emp set ename = '叶良承' where id = 1001";
            int count = stmt.executeUpdate(sql);
            //4.返回的count代表sql语句影响的行数
            System.out.println(count);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
